package org.force4spring.support;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Implementation of <code>Map</code> which indexes specified <code>Collection</code> of
 * <code>SObjectDynaBean</code>s by specified property.
 * 
 * @author Max Rudman
 */
public class SObjectDynaBeanIndex extends HashMap<Object,SObjectDynaBean> {
	public SObjectDynaBeanIndex(String keyProperty) {
		this(keyProperty, null);
	}
	
	public SObjectDynaBeanIndex(String keyProperty, Collection<SObjectDynaBean> beans) {
		if (beans == null) {
			return;
		}
		Iterator<SObjectDynaBean> itr = beans.iterator();
		while (itr.hasNext()) {
			SObjectDynaBean bean = itr.next();
			Object key = bean.get(keyProperty);
			put(key, bean);
		}
	}
}
